package com.dididoctor.doctor.Activity.Usercentre.MyPurse.CollectionMethods;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dididoctor.doctor.Dialog.DeleteConfirmDialog;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.R;

import java.util.List;

/**
 * 张强
 * Created by Administrator on 2016/4/19.
 */
public class CollectionMethodsActivity extends EduActivity implements CollectionMethodsView {
    private TextView mTvTitle, mTvFinish;
    private LinearLayout mLayAddCard;
    private ListView mlvBank;
    private CollectionMethodsAdapter adapter;
    private CollcetionMethodsPresenter presenter;
    private boolean isEditor = false;
    private List<BankBean> data;
    private Button mBtnDelete;
    private BankBean bean = null;
    private  DeleteConfirmDialog dialog;
    public static int TO_ADD_CARD = 1001;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_collection_methods);
    }

    @Override
    public void init() {

        /**头部**/
        initTitleView();

        /** 界面**/
        initview();

    }

    private void initview() {
        mLayAddCard = (LinearLayout) findViewById(R.id.lay_add_card);
        mLayAddCard.setOnClickListener(this);

        mlvBank = (ListView) findViewById(R.id.lv_bank);
        mBtnDelete = (Button) findViewById(R.id.btn_delete);
        mBtnDelete.setOnClickListener(this);

        presenter = new CollcetionMethodsPresenter(CollectionMethodsActivity.this, this);
        presenter.lunch();
    }

    private void initTitleView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("设置收款方式");
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mTvFinish.setOnClickListener(this);
        mTvFinish.setVisibility(View.VISIBLE);
        mTvFinish.setText("编辑");
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.tv_finish:
                isEditor = !isEditor;
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).setEditor(isEditor);
                    data.get(i).setIsChoice(false);
                    bean = null;
                }
                cancelEditor();
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.btn_confirm:

                break;
            case R.id.lay_add_card:
                Intent intent = new Intent(CollectionMethodsActivity.this,AddCollectionMethodsActivity.class);
                startActivityForResult(intent, TO_ADD_CARD);
                break;
            case R.id.btn_delete:
                if (bean != null) {
                   dialog = new DeleteConfirmDialog(CollectionMethodsActivity.this, bean);
                    dialog.show();
                }else{
                    showToastMessage("请选择您要删除的银行卡");
                }
                break;
        }

    }

    @Override
    public void getdoctorfail() {
    }

    @Override
    public void getdoctorsucced(List<BankBean> banks) {
        if (banks.size() == 0) {
            mTvFinish.setVisibility(View.GONE);
        } else {
            mTvFinish.setVisibility(View.VISIBLE);
        }
        data = banks;
        adapter = new CollectionMethodsAdapter(CollectionMethodsActivity.this, data);
        mlvBank.setAdapter(adapter);
    }

    @Override
    public void updateData(int tag) {
        for (int i = 0; i < data.size(); i++) {
            if (tag == i) {
                data.get(i).setIsChoice(true);
            } else {
                data.get(i).setIsChoice(false);
            }
        }
        bean = data.get(tag);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void delFinish() {
        showToastMessage("删除收款方式成功");
        dialog.dismiss();
        isEditor = !isEditor;
        cancelEditor();
        presenter.lunch();
    }

    @Override
    public void delConfirm(BankBean bean) {
        presenter.delete(bean.getOutlayId());
    }

    public void cancelEditor() {
        if (isEditor) {
            mTvFinish.setText("取消");
            mBtnDelete.setVisibility(View.VISIBLE);
            mLayAddCard.setVisibility(View.GONE);
        } else {
            mTvFinish.setText("编辑");
            mBtnDelete.setVisibility(View.GONE);
            mLayAddCard.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        presenter.lunch();
        super.onActivityResult(requestCode, resultCode, data);
    }
}
